package com.jianyun.jyzs.dao;

import android.content.Context;

import com.j256.ormlite.dao.Dao;
import com.jianyun.jyzs.bean.BadgeBean;
import com.jianyun.jyzs.db.DatabaseHelper;


import java.sql.SQLException;
import java.util.List;

/**
 * Created by yangshijun on 2018/7/18.
 * 桌面数字的数据库表操作
 */

public class BadgeDao {
    private Context context;
    private DatabaseHelper helper;
    private Dao badgeDao;

    public BadgeDao(Context context) {
        this.context = context;
        try {
            helper = DatabaseHelper.getHelper(context);
            badgeDao = helper.getDao(BadgeBean.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void insert(BadgeBean badgeBean) {
        try {
            badgeDao.createOrUpdate(badgeBean);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public BadgeBean queryByUserid(String userId) {
        //QueryBuilder<BadgeBean, Integer> builder = badgeDao.queryBuilder();
        try {
            List<BadgeBean> list = badgeDao.queryForEq("userId", userId);
            if (list!=null&&list.size()>0){
                return list.get(0);
            }else {
                return null;
            }
            //builder.where().eq("userId", userId);
            //List<BadgeBean> list = builder.query();
            //return list;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    public boolean isExist(String userId){
        List<BadgeBean> list = null;
        try {
            list = badgeDao.queryForEq("userId", userId);
            if (list!=null&&list.size()>0){
                return true;
            }else {
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }

    }

    public void updateBean(BadgeBean badgeBean){
        try {
            int update = badgeDao.update(badgeBean);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
